package com.card.client.view;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Grid;

public abstract class MouseEventGrid extends Grid {

	public MouseEventGrid() {
		super();
		// this.setBorderWidth(1);
		setCellPadding(2);
		setCellSpacing(0);
		// 增加鼠标事件。
		sinkEvents(Event.MOUSEEVENTS);
	}

	public void onBrowserEvent(Event event) {
		Element td = DOM.eventGetTarget(event);
		Element tr = DOM.getParent(td);
		Element body = DOM.getParent(tr);
		int row = DOM.getChildIndex(body, tr);
		if (row == 0){
			return;
		}
		String index = getText(row, 0);
		try {
			int id = Integer.parseInt(index);
			if (id <= 0)
				return;
		} catch (NumberFormatException nfe) {
			return;
		}

		switch (DOM.eventGetType(event)) {
		case Event.ONMOUSEOVER:
			onMouseOver(td, row);
			break;
		case Event.ONMOUSEOUT:
			onMouseOut(td, row);
			break;
		}
		super.onBrowserEvent(event);
	}

	public abstract void onMouseOver(Element td, int row);

	public abstract void onMouseOut(Element td, int row);

}
